<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2024/1/10
  Time: 12:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
    <title>Title</title>
    <style>
        img{
            width: 200px;
            height: 100px;
        }
    </style>
</head>
<body>
<div id="error"></div>
<div id="dic"></div>
</body>
</html>
<script src="js/jquery.min.js"></script>
<script>
    let html = "";
    const dic = $("#dic");
    const carousel_error = $("#error");
    window.onload = function (){
        getCarousel();
    }
    function getCarousel() {
        $.ajax({
            type: "post",
            url: "carousel/getCarousel",
            dataType: "json",
            data: {},
            success:function (res){
                if (res.msg == "success"){
                    for (var i = 0; i < res.count;i++){
                        html = "<img src='../" + res.data[i].carouselImg + "' >"+
                            "<input type='file' class='carousel_img''>"+
                        "<button onclick='addCarousel("+res.data[i].carouselId+")'>添加</button>";
                        dic.append(html);
                    }
                }else if (res.msg == "fail"){
                    carousel_error.text("没有图片");
                }
            },
            error:function(){
                alert("error");
            }
        })
    }
    function addCarousel(carouselId){
        const file = $(this).prev("input").files[0];
        console.log(file)
        $.ajax({
            type: "post",
            url: "carousel/addCarousel",
            data: {
                carouselId:carouselId,
                file:file
            },
            success:function(res){
                if (res == "success"){
                    getCarousel();
                }else if (res == "fail"){
                    console.log("添加失败");
                }else if (res == "NoFound"){
                    console.log("未找到");
                }
            },
            error:function (){
                alert("error");
            }
        })
    }
</script>
